//
//  JOLockView.m
//  手势解锁
//
//  Created by song on 15/3/9.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "JOLockView.h"


@interface JOLockView()

@property(nonatomic, strong )NSMutableArray *selectedBtns;

@property(nonatomic ,assign) CGPoint currentMovePoint;

@end

@implementation JOLockView


/**
 *  通过代码创建view
 *
 *  @param frame <#frame description#>
 *
 *  @return <#return value description#>
 */
-(instancetype)initWithFrame:(CGRect)frame
{
    self =[super initWithFrame:frame];
    if (self) {
         [self setup];
    }
    return self;
}

-(NSMutableArray *)selectedBtns
{
    if(_selectedBtns ==nil)
    {
        _selectedBtns  = [NSMutableArray array];
        
    }
    return _selectedBtns;
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
    if(self=[super initWithCoder:aDecoder])
    {
        [self setup];
    }
    return self;
}

-(void)setup
{
    for(int index =0 ;index<9 ;index++)
    {
        UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
        
        btn.userInteractionEnabled =NO;
        [btn  setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
        
        [btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];
        
        btn.tag = index;
        
        [self addSubview:btn];
    }
}

-(void)layoutSubviews
{
    for (int index =0 ; index<self.subviews.count; index++) {
        UIButton *btn =self.subviews[index];
        CGFloat btnW = 74;
        CGFloat btnH = 74;
        
        int totalCol = 3;
        int col =  index % totalCol;
        int row = index /totalCol;
        
        CGFloat marginX = (self.frame.size.width  -totalCol*btnW )/(totalCol+1);
        
        CGFloat marginY = marginX;
        
        CGFloat btnX = marginX +col *(btnW +marginX);
        CGFloat btnY = row *(btnH + marginY);
        
        btn.frame =CGRectMake(btnX, btnY, btnW, btnH);
        
    }
}

-(void)drawRect:(CGRect)rect
{
    if (self.selectedBtns ==nil ||self.selectedBtns.count ==0) {
        return;
    }
    
    UIBezierPath *path =[UIBezierPath bezierPath];
    
    for(int index = 0 ;index <self.selectedBtns.count; index++)
    {
        UIButton *btn = self.selectedBtns[index];
        if(index ==0)
        {
            [path moveToPoint:btn.center];
        }else{
            [path addLineToPoint:btn.center];
        }
    }
    
    [path addLineToPoint:self.currentMovePoint];
    path.lineJoinStyle =kCGLineJoinBevel;
    path.lineCapStyle =kCGLineJoinBevel;
    
    path.lineWidth = 8 ;
    [[UIColor greenColor] set];
    [path stroke];
    
    
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     [self thummove:touches];
}



-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self thummove:touches];
}


-(void)thummove:(NSSet *)touches
{
    UITouch *touch = [touches anyObject];
    
    CGPoint pos = [touch locationInView:touch.view];
    
    for(UIButton *btn in self.subviews)
    {
        if(CGRectContainsPoint(btn.frame, pos) && btn.selected ==NO )
        {
            btn.selected = YES;
            [self.selectedBtns addObject:btn];
        }else{
            self.currentMovePoint = pos;
        }
    }
    
    [self setNeedsDisplay];
}


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    NSMutableString *mstr = [NSMutableString string];
    
    for(UIButton *btn in self.selectedBtns)
    {
        [mstr appendFormat:@"%d",btn.tag];
    }
    NSLog(@"%@",mstr);
    [self.selectedBtns makeObjectsPerformSelector:@selector(setSelected:) withObject:@(NO)];
    [self.selectedBtns removeAllObjects];
    [self setNeedsDisplay];
    self.currentMovePoint = CGPointZero;
}

@end
